import logging

from xadmin.model.objects import AttributeValueException
from xadmin.lib.administrators import ItemAdministrator
from xadmin.lib.base import *
from xadmin.lib.sqlalchemy_driver import User

log = logging.getLogger(__name__)

class XusersController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('xuser', 'xusers')

    def __init__(self):
        self.admin = ItemAdministrator(User)
        self.resource = 'xusers'


    def index(self, format='html'):
        """GET /xusers: All items in the collection."""
        # url_for('xusers')
        c.items_url = "/%s" % self.resource
        c.items = self.admin.getAll()
        response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        return render('/%s/list.mako' % self.resource)

    def create(self):
        """POST /xusers: Create a new item."""
        # url_for('xusers')
        try:
            content = request.body.read(int(request.environ['CONTENT_LENGTH']))
            itemid = self.admin.create(content)
            self.response_create()
            return "/%s/%s" %(self.resource, itemid)
        except AttributeValueException, e:
            abort(400, e.message)

    def new(self, format='html'):
        """GET /xusers/new: Form to create a new item."""
        # url_for('new_xuser')
        pass

    def update(self, id):
        """PUT /xusers/id: Update an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(h.url_for('xuser', id=ID),
        #           method='put')
        # url_for('xuser', id=ID)
        try:
            content = request.body.read(int(request.environ['CONTENT_LENGTH']))
            itemid = self.admin.update(id, content)
            self.response_update()
            return "/%s/%s" %(self.resource, itemid)
        except AttributeValueException, e:
            abort(400, e.message)

    def delete(self, id):
        """DELETE /xusers/id: Delete an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="DELETE" />
        # Or using helpers:
        #    h.form(h.url_for('xuser', id=ID),
        #           method='delete')
        # url_for('xuser', id=ID)
        self.admin.delete(id)
        self.response_delete()

    def show(self, id, format='html'):
        """GET /xusers/id: Show a specific item."""
        # url_for('xuser', id=ID)
        c.item = self.admin.getById(id)
        if c.item:
            response.headers['Content-Type'] = 'text/xml; charset=utf-8'
            return render('/%s/view.mako' % self.resource)
        else:
            abort(404)

    def edit(self, id, format='html'):
        """GET /xusers/id;edit: Form to edit an existing item."""
        # url_for('edit_xuser', id=ID)
        pass
